<template>
  <div>
    <el-row>
      <el-col :span="6" class="logo-container">
        <img src="../assets/logo.png" class="logo" alt />
        <span class="title">校园点餐后台管理系统</span>
      </el-col>
      <el-col :span="6" class="user">
        <div class="userinfo">
          <div class="welcome">
            <p class="name comename">欢迎</p>
            <p class="name avatarname">{{ userInfo.name }}</p>
          </div>
          <span class="username">
            <el-dropdown trigger="click" @command="setDialogInfo">
              <span class="el-dropdown-link">
                <i class="el-icon-caret-bottom el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="editpwd">修改密码 </el-dropdown-item>
                <el-dropdown-item command="userInfo">个人信息</el-dropdown-item>
                <el-dropdown-item command="logout">退出</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </span>
        </div>
      </el-col>
    </el-row>
    <!-- 弹出框 -->
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="50%"
      :before-close="handleClose"
    >
      <el-form
        ref="userFormRef"
        :model="userForm"
        :rules="userFromRules"
        label-width="120px"
      >
        <el-form-item label="姓名" prop="name">
          <el-input :readonly="readonlyFlag" v-model="userForm.name"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-input :readonly="readonlyFlag" v-model="userForm.sex"></el-input>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input :readonly="readonlyFlag" v-model="userForm.age"></el-input>
        </el-form-item>
        <el-form-item label="身份证号" prop="number">
          <el-input
            disabled
            :readonly="readonlyFlag"
            v-model="userForm.number"
          ></el-input>
        </el-form-item>
        <el-form-item label="电话号码" prop="telepnone">
          <el-input
            :readonly="readonlyFlag"
            v-model="userForm.telephone"
          ></el-input>
        </el-form-item>
        <el-form-item
          v-if="userInfo.role === 'student'"
          label="地址"
          prop="UserAddress"
        >
          <el-input
            :readonly="readonlyFlag"
            v-model="userForm.UserAddress"
          ></el-input>
        </el-form-item>
        <el-form-item label="角色" prop="role">
          <el-select disabled v-model="userForm.role" placeholder="请选择角色">
            <el-option
              label="管理员"
              value="admin"
              title="您无法注册管理员"
            ></el-option>
            <el-option label="学生" value="student"></el-option>
            <el-option label="投资人" value="invest"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="editClick">{{
          tempFlag ? "修 改" : "保 存"
        }}</el-button>
        <el-button v-if="tempFlag" type="info" @click="closeDialog"
          >关 闭</el-button
        >
      </span>
    </el-dialog>
    <!-- 修改密码弹出框 -->
    <el-dialog
      title="提示"
      :visible.sync="pwdDialog"
      width="50%"
      :before-close="handleClose"
    >
      <el-form
        ref="editPwdFormRef"
        :model="editPwdForm"
        :rules="editPwdFromRules"
        label-width="120px"
      >
        <el-form-item label="原密码" prop="oldpwd">
          <el-input type="password" v-model="editPwdForm.oldpwd"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="newpwd">
          <el-input type="password" v-model="editPwdForm.newpwd"></el-input>
        </el-form-item>
        <el-form-item label="确认新密码" prop="newpwdtwo">
          <el-input type="password" v-model="editPwdForm.newpwdtwo"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="editPwdClick">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: 'head-nav',
  data () {
    return {
      dialogVisible: false,
      pwdDialog: false,
      readonlyFlag: true,
      tempFlag: true,
      editPwdForm: {},
      userForm: {},
      userFromRules: {
        name: [{ required: true, message: '姓名不能为空', trigger: 'blur' }],
        number: [
          { required: true, message: '身份证号码不能为空', trigger: 'blur' },
          {
            validator: function (rule, value, callback) {
              if (
                /^[1-9]\d{5}(18|19|20|(3\d))\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/.test(
                  value
                ) == false
              ) {
                callback(new Error('请输入正确的身份证号'))
              } else {
                callback()
              }
            },
            trigger: 'blur'
          }
        ],
        telephone: [
          { required: true, message: '电话不能为空', trigger: 'blur' },
          {
            validator: function (rule, value, callback) {
              if (/^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-7|9])|(?:5[0-3|5-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[1|8|9]))\d{8}$/.test(value) == false) {
                callback(new Error('请输入正确的手机号'))
              } else {
                callback()
              }
            },
            trigger: 'blur'
          }
        ],
       UserAddress: [{ required: true, message: '地址不能为空', trigger: 'blur' }],
      },
      editPwdFromRules: {
        oldpwd: [{ required: true, message: '密码不能为空', trigger: 'blur' }],
        newpwd: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, max: 18, message: '密码在6-18位之间', trigger: 'blur' }
        ],
        newpwdtwo: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, max: 18, message: '密码在6-18位之间', trigger: 'blur' }
        ]
      }
    }
  },
  mounted () {
    this.getUserList()
  },
  computed: {
    userInfo () {
      return this.$store.getters.userInfo
    }
  },
  methods: {
    getUserList () {
      this.userForm = { ...this.userInfo }
      if (this.userForm.role === 'student') {
        this.userForm.role = '学生'
      }
      if (this.userForm.role === 'invest') {
        this.userForm.role = '老师'
      }
      if (this.userForm.role === 'admin') {
        this.userForm.role = '管理员'
      }
    },
    editPwdClick () {
      if (this.editPwdForm.newpwd != this.editPwdForm.newpwdtwo) {
        return this.$message.error('确认密码和密码不一致，请重新输入')
      }
      this.$api
        .post(`/api/user/editpwd/${this.userInfo._id}`, this.editPwdForm)
        .then(res => {
          this.$message.success('修改成功')
          this.pwdDialog = false
          this.editPwdForm = {}
        })
        .catch(err => {
          this.$message.error('修改失败,原始密码错误')
        })
    },
    setDialogInfo (cmditem) {
      if (!cmditem) {
        this.$message('菜单选项缺少command属性')
        return
      }
      switch (cmditem) {
        case 'editpwd':
          this.pwdDialog = true
          break
        case 'userInfo':
          this.dialogVisible = true
          this.getUserList()
          break
        case 'logout':
          this.logout()
          break
      }
    },
    handleClose (done) {
      done()
    },
    editClick () {
      if (this.tempFlag === true) {
        this.tempFlag = false
        this.readonlyFlag = false
      } else {
        // console.log(this.userForm.role, 'dsada')
        this.userForm.role === '管理员'
          ? (this.userForm.role = 'admin')
          : this.userForm.role === '老师'
            ? (lthis.userForm.role = 'invest')
            : this.userForm.role === '学生'
              ? (this.userForm.role = 'student')
              : this.userForm.role;
        this.$api
          .post(`/api/user/edit/${this.userForm._id}`, this.userForm)
          .then(res => {
            console.log(res.config.data,"res")
            this.tempFlag = true
            this.readonlyFlag = true
          })
          .catch(err => {
            this.$message.error('修改用户信息失败')
          })
      }
    },
    closeDialog () {
      this.dialogVisible = false
      this.$router.go(0)
    },
    logout () {
      // 清除token
      localStorage.removeItem('eleToken')
      this.$store.dispatch('clearCurrentState')
      // 页面跳转
      this.$router.push('/login')
      this.$message.warning('已退出登录')
    }
  }
}
</script>

<style scoped>
.el-row {
  height: 80px;
}
.logo-container {
  line-height: 60px;
  min-width: 400px;
}
.logo {
  height: 50px;
  width: 50px;
  margin-right: 5px;
  vertical-align: middle;
  display: inline-block;
}
.title {
  vertical-align: middle;
  font-size: 22px;
  font-family: "Microsoft YaHei";
  letter-spacing: 3px;
  color: #000;
}
.user {
  line-height: 60px;
  text-align: right;
  float: right;
  padding-right: 10px;
}
.avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  vertical-align: middle;
  display: inline-block;
}
.welcome {
  display: inline-block;
  width: auto;
  vertical-align: middle;
  padding: 0 5px;
}
.name {
  line-height: 20px;
  text-align: center;
  font-size: 14px;
}
.comename {
  font-size: 12px;
  color:#000;
}
.avatarname {
  color: #409eff;
  font-weight: bolder;
}
.username {
  cursor: pointer;
  margin-right: 5px;
}
.el-dropdown {
  color: #000;
}
</style>
